package dao;

import entity.MergeEntity;
import entity.PageEntity;
import entity.VegetablesImportRecord;
import org.apache.commons.collections.map.HashedMap;
import org.apache.log4j.Logger;
import util.IRowMapper;

import javax.servlet.jsp.PageContext;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/11/10.
 */
public class VegetableImportDao {
    private DBHelper dbHelper=new DBHelper();
    //得到总行数
    public int pageCounts()throws ClassNotFoundException,SQLException{
        String sql="select count(1) from vegetablesimportrecord";
        Object object=dbHelper.querySingle(sql);
        return (int)((Long)object).longValue();
    }
    //显示蔬菜进口单
    public List<MergeEntity> queryAll(PageEntity pe)throws ClassNotFoundException,SQLException{
        String sql="SELECT  vr.id importid,vs.name kind,vr.quantity quantity FROM \n" +
                "vegetablesimportrecord vr,vegetablestypes vs WHERE vr.typeid=vs.id limit ?,?";
        //设置两个？的值
        int a=(pe.getPageIndex()-1)*pe.getPageSize();
        int b=pe.getPageSize();
        IRowMapper<MergeEntity> rowmapper=new IRowMapper<MergeEntity>() {
            @Override
            public MergeEntity mapper(ResultSet rs) throws SQLException, ClassNotFoundException {
                MergeEntity me=new MergeEntity();
                me.setVid(rs.getString("importid"));
                me.setName(rs.getString("kind"));
                me.setQuantity(rs.getFloat("quantity"));
                return me;
            }
        };
        List<MergeEntity> list=dbHelper.queryAllHelper(rowmapper,sql,a,b);
        return list;


    }
    //根据订单编号删除信息
    public int delete(String id)throws  ClassNotFoundException,SQLException{
        String sql="DELETE  FROM vegetablesimportrecord WHERE id=?";
        int result=dbHelper.executeUpdate(sql,id);
        return result;
    }
    //修改蔬菜进口单
    public int modify(String id1,float quantity,int typeid)throws ClassNotFoundException,SQLException{
        String sql="UPDATE vegetablesimportrecord vi SET vi.quantity=?,\n" +
                "vi.typeid=? WHERE vi.id=?";
        return dbHelper.executeUpdate(sql,quantity,typeid,id1);

    }

    //关闭
    public void close()throws SQLException{
        dbHelper.close();
    }
}
